package de.justphil.tcg.tcgserver.commons.domain;


import java.io.Serializable;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import de.justphil.tcg.tcgserver.commons.util.DateTimeUtil;

@Entity
@Table(name = "users", schema = "tcg")
@XmlRootElement
@XmlAccessorType(XmlAccessType.NONE)
public class User implements Serializable {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -7652024096831897967L;

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@Column(name = "user_id")
	@XmlElement
	private long id;
	
	@Column(name = "admin")
	private boolean admin;
	
	@Column(name = "username")
	@XmlElement
	private String username;
	
	@Column(name = "password")
	@XmlElement
	private String password;
	
	/*
    @XmlElement
    @ManyToMany(mappedBy="playerList", fetch=FetchType.EAGER)
	*/
	
	@JoinTable(name = "game_players", joinColumns = {
	        @JoinColumn(name = "user_id", referencedColumnName = "user_id")}, inverseJoinColumns = {
	        @JoinColumn(name = "game_id", referencedColumnName = "game_id")})
    @ManyToMany(fetch=FetchType.EAGER)
    private Set<Game> games;
    
    //@OneToMany(mappedBy = "user", fetch=FetchType.EAGER)
    @OneToMany(fetch=FetchType.EAGER)
    @JoinColumn(name="user_id", referencedColumnName="user_id")
    private Set<CardInstance> cardInstances;
    
    @OneToMany(fetch=FetchType.EAGER)
    @JoinColumn(name="user_id", referencedColumnName="user_id")
    private Set<Deck> decks;
	
	@Column(name = "created")
	@Temporal(TemporalType.TIMESTAMP)
	private Date created;
	
	@Column(name = "modified")
	@Temporal(TemporalType.TIMESTAMP)
	private Date modified;

	

	public User() {
		// JPA requires a default constructor without parameters
		
		this.games			= new HashSet<Game>();
		this.cardInstances	= new HashSet<CardInstance>();
		this.decks			= new HashSet<Deck>();
	}

	public User(String username, String password, boolean admin) {
		this();
		
		this.admin = admin;
		this.username = username;
		this.password = password;
		
		long nowUTC = DateTimeUtil.getUTCTimeInMillis();
		this.created = new Date(nowUTC);
		this.modified = new Date(nowUTC);
	}
	
	public long getId() {
		return id;
	}

	public boolean isAdmin() {
		return admin;
	}

	public String getUsername() {
		return username;
	}

	public String getPassword() {
		return password;
	}
	
	public Set<Game> getPlayedGames() {
		return games;
	}

	public Set<CardInstance> getCardInstances() {
		return cardInstances;
	}

	public Set<Deck> getDecks() {
		return decks;
	}

	public Date getCreated() {
		return created;
	}

	public Date getModified() {
		return modified;
	}
	
	
	

	public void setId(long id) {
		this.id = id;
	}
	
	public void setAdmin(boolean admin) {
		this.admin = admin;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public void setPassword(String password) {
		this.password = password;
	}
	
	public void setPlayedGames(Set<Game> gameList) {
		this.games = gameList;
	}

	public void setCardInstances(Set<CardInstance> cardInstances) {
		this.cardInstances = cardInstances;
	}

	public void setDecks(Set<Deck> decks) {
		this.decks = decks;
	}

	public void setCreated(Date created) {
		this.created = created;
	}

	public void setModified(Date modified) {
		this.modified = modified;
	}
	
	/* ############################################################################################ */
	/* ############################################################################################ */
	/* ############################################################################################ */
	
	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder(255);
		
		sb.append("User [id=").append(id).append(", username=").append(username).append(", password=").append(password).append("]\n");
		sb.append("\tGameList (").append(games.size()).append(") {\n");
		
		int i = 0;
		for (Game g : games) {
			sb.append("\t\t").append(g.toString());
			
			if ( i < (games.size()-1) ) {
				sb.append(",\n");
			}
			else {
				sb.append("\n");
			}
			
			i++;
		}
		
		sb.append("\t}");
		
		return sb.toString();
	}

	
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + (int) (id ^ (id >>> 32));
		result = prime * result
				+ ((username == null) ? 0 : username.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		User other = (User) obj;
		if (id != other.id)
			return false;
		if (username == null) {
			if (other.username != null)
				return false;
		} else if (!username.equals(other.username))
			return false;
		return true;
	}
	
	
	/* ############################################################################################ */
	/* ############################################################################################ */
	/* ############################################################################################ */

	/*
	@Override
	public void readData(DataInput in) throws IOException {
		id = in.readLong();
		username = in.readUTF();
		password = in.readUTF();
		created = new Date(in.readLong());
		modified = new Date(in.readLong());
	}

	@Override
	public void writeData(DataOutput out) throws IOException {
		out.writeLong(id);
		out.writeUTF(username);
		out.writeUTF(password);
		out.writeLong(created.getTime());
		out.writeLong(modified.getTime());
	}
	*/



	public class ManagerId {
		public static final long LOCATION	= 1L;
		public static final long GAME_DRAW	= 2L;
	}
	
}
